package de.ipk_gatersleben.bit.bi.edal.publication;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.nio.file.Paths;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.swing.JButton;
import javax.swing.JPanel;

import de.ipk_gatersleben.bit.bi.edal.primary_data.metadata.EnumDublinCoreElements;
import de.ipk_gatersleben.bit.bi.edal.primary_data.metadata.MetaData;
import de.ipk_gatersleben.bit.bi.edal.primary_data.metadata.MetaDataException;
import de.ipk_gatersleben.bit.bi.edal.publication.listener.ShutdownWindowsListener;
import de.ipk_gatersleben.bit.bi.edal.publication.metadata.MetaDataCollector;
import de.ipk_gatersleben.bit.bi.edal.publication.metadata.ProgressBarDialog;

public class PublicationButtonLinePanel extends JPanel implements
		ActionListener {

	private static final long serialVersionUID = -6295685143671113656L;
	private JButton SUBMIT_BUTTON = new JButton("SUBMIT");
	private JButton CANCEL_BUTTON = new JButton("CANCEL");
	public static boolean updatePublicationFlag = false;

	public PublicationButtonLinePanel() {

		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));

		buttonPanel.add(SUBMIT_BUTTON);
		buttonPanel.add(CANCEL_BUTTON);
		buttonPanel.setBackground(PropertyLoader.HEADER_FOOTER_COLOR);

		SUBMIT_BUTTON.addActionListener(this);
		CANCEL_BUTTON.addActionListener(this);

		this.setBackground(PropertyLoader.HEADER_FOOTER_COLOR);
		this.add(buttonPanel, BorderLayout.SOUTH);
	}

	protected JButton getSubmitButton() {
		return this.SUBMIT_BUTTON;
	}

	@Override
	public void actionPerformed(ActionEvent actionEvent) {

		boolean everythingCorrect = true;

		if (actionEvent.getSource().equals(SUBMIT_BUTTON)) {

			if (PublicationMainPanel.uploadPathField.getText()
					.equalsIgnoreCase(
							PublicationMainPanel.DEFAULT_UPLOAD_PATH_STRING)) {

				PropertyLoader.UPLOADPATH_LABEL.setForeground(Color.RED);
				PublicationFrame.updateUI();
				everythingCorrect = false;
			}
			if (Utils
					.cleanUpString(PublicationMainPanel.titleField.getText())
					.equalsIgnoreCase(
							Utils.cleanUpString(PublicationMainPanel.DEFAULT_TITLE_STRING))) {

				PropertyLoader.TITLE_LABEL.setForeground(Color.RED);
				PublicationFrame.updateUI();
				everythingCorrect = false;
			}

			if (Utils
					.cleanUpString(
							PublicationMainPanel.descriptionField.getText())
					.equalsIgnoreCase(
							Utils.cleanUpString(PublicationMainPanel.DEFAULT_DESCRIPTION_STRING))) {

				PropertyLoader.DESCRIPTION_LABEL.setForeground(Color.RED);
				PublicationFrame.updateUI();
				everythingCorrect = false;
			}

			if (Utils
					.cleanUpString(PublicationMainPanel.authorsField.getText())
					.equalsIgnoreCase(
							Utils.cleanUpString(PublicationMainPanel.DEFAULT_AUTHORS_STRING))) {

				PropertyLoader.AUTHORS_LABEL.setForeground(Color.RED);
				PublicationFrame.updateUI();
				everythingCorrect = false;
			}

			if (Utils.checkIfStringIsEmpty(PublicationMainPanel.publisherField
					.getText())) {

				PropertyLoader.PUBLISHER_LABEL.setForeground(Color.RED);
				PublicationFrame.updateUI();
				everythingCorrect = false;
			}

			if (Utils
					.cleanUpString(PublicationMainPanel.subjectsField.getText())
					.equalsIgnoreCase(
							Utils.cleanUpString(PublicationMainPanel.DEFAULT_SUBJECTS_STRING))) {

				PropertyLoader.SUBJECTS_LABEL.setForeground(Color.RED);
				PublicationFrame.updateUI();
				everythingCorrect = false;
			}

			if (everythingCorrect) {

				MetaDataCollector collector = new MetaDataCollector();
				collector.collectAllMetaData();

				MetaData metaData = PublicationModul.clientDataManager
						.createMetadataInstance();

				try {
					metaData.setElementValue(EnumDublinCoreElements.CREATOR,
							collector.getCreators());
					metaData.setElementValue(
							EnumDublinCoreElements.CONTRIBUTOR,
							collector.getContributors());
					metaData.setElementValue(EnumDublinCoreElements.SUBJECT,
							collector.getSubjects());
					metaData.setElementValue(EnumDublinCoreElements.CREATOR,
							collector.getCreators());
					metaData.setElementValue(EnumDublinCoreElements.LANGUAGE,
							collector.getLanguage());
					metaData.setElementValue(
							EnumDublinCoreElements.DESCRIPTION,
							collector.getDescription());
					metaData.setElementValue(EnumDublinCoreElements.PUBLISHER,
							collector.getPublisher());
					metaData.setElementValue(EnumDublinCoreElements.TITLE,
							collector.getTitle());
				} catch (MetaDataException e) {
					e.printStackTrace();
				}

				try {

					ProgressBarDialog dialog = new ProgressBarDialog(
							PublicationModul.getFrame(),
							Paths.get(PublicationMainPanel.uploadPathField
									.getText()), metaData,
							PublicationFrame.getUserDirectory(),
							new InternetAddress(PublicationFrame.loggedUser));

					dialog.showDialog();

					if (dialog.getReturnValue() == 1) {
						PublicationMainPanel.reset();
					}

				} catch (AddressException e) {
					e.printStackTrace();
				}

			}

		} else if (actionEvent.getSource().equals(CANCEL_BUTTON)) {
			ShutdownWindowsListener shutdown = new ShutdownWindowsListener();
			shutdown.windowClosing(null);
		}
	}

}
